<?php
	function drl_add() {
		global $kiwee;

		$name = addslashes(trim($_POST['name']));
		$desc = addslashes(trim($_POST['description']));
		
		if (strlen($name) < 1)
			die('ERROR: Please supply a valid name.');
		
		$qry = 'insert into ' . $kiwee->config['fpx'] . 'discountlists (name, description) values ("' . $name . '","' . $desc . '")';
		mysql_query($qry);
	}		

	function drl_delete() {
		global $kiwee;

		$id = intval($_POST['listid']);
		
		$qry = 'delete from ' . $kiwee->config['fpx'] . 'discountlists where id=' . $id;
		mysql_query($qry);
	}		

	function drl_gridload() {
		global $kiwee, $_lang;
		
		$sql = 'SELECT * FROM ' . $kiwee->config['fpx'] . 'discountlists order by name';
		$ds = mysql_query($sql) or die ('ERROR: ' . mysql_error());
		$grd = new DataGrid('',$ds, $number_of_results);
		$grd->noRecordMsg = $_lang['no_records_found'];
		$grd->cssClass='grid';
		$grd->columnHeaderClass='gridHeader';
		$grd->itemClass='gridItem';
		$grd->altItemClass='gridAltItem';
		$grd->fields='name, description';
		$grd->columns= $_lang['name'] . ', ' . $_lang['description'] . ',' . $_lang['kiwee_properties'] . ',' . 
		  				$_lang['kiwee_entries'] . ',' . $_lang['delete'];
		$grd->colWidths='100,300,50,50,50';
		$grd->colAligns='left,left,center,center,center';
		$grd->colTypes=',,template:<button onClick="drl_properties([+id+])">' . $_lang['kiwee_properties'] . '</button>,' .
					   'template:<button onClick="drl_entries([+id+])">' . $_lang['kiwee_entries'] . '</button>,' .
					   'template:<button onClick="drl_delete([+id+])">' . $_lang['delete'] . '</button>';
		echo $grd->render();
	}

	function drl_properties() {
		global $kiwee, $_lang;
		
		$listid = intval($_POST['listid']);
		
		$qry = 'select * from ' . $kiwee->config['fpx'] . 'discountlists where id=' . $listid;
		$rows = db_getrows($qry);
		if (sizeof($rows) < 1) die('ERROR: ' . mysql_error());

		$lines = array();
		$lines[] = '<table class="lbform"><tbody>';
		$lines[] = '<tr><td><label for="listname">' . $_lang['name'] . ':</label></td>';
		$lines[] = '<td width=20>&nbsp;</td>';
		$lines[] = '<td><input id="listname" name="listname" type="text" value="' . $rows[0]['name'] . '" length=20 /></td></tr>';
		$lines[] = '<tr><td><label for="listdesc">' . $_lang['description'] . ':</label></td>';
		$lines[] = '<td width=20>&nbsp;</td>';
		$lines[] = '<td><input id="listdesc" name="listdesc" type="text" value="' . $rows[0]['description'] . '" length=20 /></td></tr>';
		$lines[] = '</tbody></table>';
		$lines[] = '<button onclick="drl_update(' . $listid . ')">' . $_lang['save'] . '</button>';
		$lines[] = '<button onclick="hideBox()">' . $_lang['cancel'] . '</button>';
		echo implode("\n", $lines);
	}

	function drl_update() {
		global $kiwee;

		$name = addslashes(trim($_POST['name']));
		$desc = addslashes(trim($_POST['desc']));
		$id = intval($_POST['id']);
		
		if (strlen($name) < 1)
		  die('ERROR: Please supply a valid name.');
		
		$qry = 'update ' . $kiwee->config['fpx'] . 'discountlists set name="' . $name . '",description="' . $desc . '" where id=' . $id;
		mysql_query($qry);
		drl_gridload();
	}

	function drl_entries() {
		global $kiwee, $_lang;
		
		$listid = intval($_POST['listid']);

		$lines = array();

		$sql = 'SELECT * FROM ' . $kiwee->config['fpx'] . 'discountlists_entries where list_id=' . $listid . ' order by minqty';
		$ds = mysql_query($sql) or die ('ERROR: ' . mysql_error());
		$grd = new DataGrid('', $ds, $number_of_results);
		$grd->noRecordMsg = $_lang['no_records_found'];
		$grd->cssClass='grid';
		$grd->columnHeaderClass='gridHeader';
		$grd->itemClass='gridItem';
		$grd->altItemClass='gridAltItem';
		$grd->fields='minqty, discount';
		$grd->columns= $_lang['kiwee_discount_minqty'] . ', ' . $_lang['kiwee_discount_discount'] . ',' . $_lang['kiwee_discount_delete'];
		$grd->colWidths='50, 50, 30';
		$grd->colAligns='left,left,center';
		$grd->colTypes=',,template:<button style="width: auto;" onClick="drl_entries_delete([+id+])">X</button>';
		$lines[] = $grd->render();
		$lines[] = '<form><input type=text id="drl_minqty" size=8 id=minqty>&nbsp;&nbsp;';
		$lines[] = '<input type=text id="drl_discrate" size=8>&nbsp;&nbsp;';
		$lines[] = '<button style="width: auto;" onclick="drl_entries_add(' . $listid . ')">' . $_lang['kiwee_discount_add'] . '</button></form>';
		echo implode("\n", $lines);
	}

	function drl_entries_add() {
		global $kiwee;
		
		$listid = intval($_POST['listid']);
		$minqty = floatval($_POST['minqty']);
		$discount = floatval($_POST['discount']);

		$qry = 'insert into ' . $kiwee->config['fpx'] . 'discountlists_entries (list_id, minqty, discount) values (' .
				$listid . ',' . $minqty . ',' . $discount . ')';
		mysql_query($qry);
		drl_entries();
	}		

	function drl_entries_delete() {
		global $kiwee;
		
		$id = intval($_POST['id']);
		
		mysql_query('delete from ' . $kiwee->config['fpx'] . 'discountlists_entries where id=' . $id);
		drl_entries();
	}		

	$oktxns[] = 'drl_add';
	$oktxns[] = 'drl_gridload';
	$oktxns[] = 'drl_delete';
	$oktxns[] = 'drl_properties';
	$oktxns[] = 'drl_update';
	$oktxns[] = 'drl_entries';
	$oktxns[] = 'drl_entries_add';
	$oktxns[] = 'drl_entries_delete';
?>